package org.komarichin.servlets.upload;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * format for xml <br/>
 * example :<br/>
 * <b>
 * &lt;response command=&quot;list&quot; currentPath=&quot;/img&quot;&gt;<br/>
 * &nbsp;&nbsp;&nbsp;&lt;file name=&quot;ex1.png&quot; id=&quot;4324&quot; length=&quot;123456&quot; contentType=&quot;image/png&quot; isDirectory=&quot;false&quot; path=&quot;/img&quot;/&gt;<br/>
 * &nbsp;&nbsp;&nbsp;&lt;file name=&quot;ex2.png&quot; id=&quot;4324&quot; length=&quot;123456&quot; contentType=&quot;image/png&quot; isDirectory=&quot;false&quot; path=&quot;/img&quot;/&gt;<br/>
 * &nbsp;&nbsp;&nbsp;&lt;file name=&quot;/image&quot; id=&quot;4324&quot;  isDirectory=&quot;true&quot; /&gt;<br/>
 * &nbsp;&nbsp;&nbsp;...<br/>
 * &nbsp;&nbsp;&nbsp;&lt;file name=&quot;exN.png&quot; id=&quot;4324&quot; length=&quot;123456&quot; contentType=&quot;image/png&quot; isDirectory=&quot;false&quot; path=&quot;/img&quot;/&gt;<br/>
 * &lt;/response&gt;
 * </b>
 * </p>
 * <p>
 * format for json<br/>
 * example: <br/>
 * <pre><b>
 * {response:{
 *      command:'list',
 *      currentPath:'/images',
 *      files:[
 *          {name:'ex1.png', id: '4324', length:'12345', contentType:'image/png', isDirectory:'false', path:'/img'},
 *          {name:'ex2.png', id: '4324', length:'12345', contentType:'image/png', isDirectory:'false', path:'/img'},
 *          {name:'ex3.png', id: '4324', length:'12345', contentType:'image/png', isDirectory:'false', path:'/img'},
 *          {name: '/image', id: '4324', isDirectory: 'true'},
 *          ....
 *          {name:'exN.png', id: '4324', length:'12345', contentType:'image/png', isDirectory:'false', path:'/img'}
 * <p/>
 *      ]
 *  }
 * }
 * </b></pre><br/>
 * example for directory<br/>
 * <b>
 * <p/>
 * </b>
 * <p/>
 * </p>
 * User: Mykhaylo Komarichyn
 * Date: 04.01.2010
 * Time: 14:23:32
 */
public class Response {
    private String command;
    private String currentPath;
    private List<ResponseFile> files = new ArrayList<ResponseFile>();

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public String getCurrentPath() {
        return currentPath;
    }

    public void setCurrentPath(String currentPath) {
        this.currentPath = currentPath;
    }

    public List<ResponseFile> getFiles() {
        return files;
    }

    public void setFiles(List<ResponseFile> files) {
        this.files = files;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Response");
        sb.append("{command='").append(command).append('\'');
        sb.append(", currentPath='").append(currentPath).append('\'');
        sb.append(", files=").append(files);
        sb.append('}');
        return sb.toString();
    }

    public String toXML() {
        if (StringUtils.isEmpty(command)) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        builder.append("<response command=\"").append(command).append("\" ")
                .append("currentPath=\"").append(currentPath).append("\" >");
        for (ResponseFile file : files) {
            builder.append(file.toXML());
        }
        builder.append("<response>");

        return builder.toString();
    }

    public String toJSON() {
        if (StringUtils.isEmpty(command)) {
            return "{}";
        }
        StringBuilder builder = new StringBuilder("{response:{");
        builder.append("command:'").append(command).append("',")
                .append("currentPath:'").append(currentPath).append("',")
                .append("files:[");
        int size = files.size();
        int inc = 0;
        for (ResponseFile file : files) {
            inc++;
            builder.append(file.toJSON());
            if (inc < size) {
                builder.append(",");
            }
        }
        builder.append("]}}");

        return builder.toString();
    }
}
